package com.blog.back.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.blog.back.config.CacheArticleLinkedList;
import com.blog.dto.PageDTO;
import com.blog.utils.R;
import com.blog.vo.ArticleDetailVO;
import com.blog.vo.ArticleListVO;
import com.blog.vo.ArticleVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.blog.entity.ArticleEntity;
import com.blog.back.service.ArticleService;
import com.blog.utils.PageUtils;
import com.blog.utils.R;

import javax.xml.crypto.Data;


/**
 * @author codedorado
 * @email codedorado@foxmail.com
 * @date 2020-08-15 15:26:05
 */
@RestController
@RequestMapping("back/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;
    @Autowired
    private CacheArticleLinkedList cacheArticleLinkedList;
    /**
     * 列表
     */
    @RequestMapping("/list")
    // @RequiresPermissions("back:article:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = articleService.queryPage(params);

        return R.ok().put("data", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{articleId}")
    // @RequiresPermissions("back:article:info")
    public R info(@PathVariable("articleId") Long articleId) {
        ArticleEntity article = articleService.getById(articleId);

        return R.ok().put("data", article);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("back:article:save")
    public R save(@RequestBody ArticleEntity article) {
        articleService.save(article);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("back:article:update")
    public R update(@RequestBody ArticleEntity article) {
        articleService.updateById(article);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("back:article:delete")
    public R delete(@RequestBody Long[] articleIds) {
        articleService.removeByIds(Arrays.asList(articleIds));

        return R.ok();
    }

    @PostMapping("/listbyuser")
    public R listByUser(PageDTO pageDTO) {
        ArticleListVO articleListVO = articleService.listByUserPage(pageDTO);
        return R.ok().put("data", articleListVO);
    }

    @RequestMapping("/listbykeywords")
    public R listByKeywords(PageDTO pageDTO) {
        ArticleListVO articleListVO = articleService.listByUserWithKeywordsPage(pageDTO);
        return R.ok().put("data", articleListVO);
    }

    @RequestMapping("/infoDetail/{usersId}/{articleId}")
    // @RequiresPermissions("back:article:info")
    public R info(@PathVariable("usersId") Long usersId,
                  @PathVariable("articleId") Long articleId){
        ArticleDetailVO vo = articleService.getArticleDetailById(articleId,usersId);

        return R.ok().put("data", vo);
    }

    /**
     * 获取点击量排行前三的文章
     * @param usersId
     * @return
     */
    @GetMapping("/info/morehit/{usersId}")
    public R inforMoreHit(@PathVariable("usersId") Long usersId) {
        List<ArticleVO> articleVOS = articleService.litsMostArticles(usersId);
        return R.ok().put("data", articleVOS);
    }

    /**
     * 根据用户id查找自我介绍
     * @param usersId
     * @return
     */
    @RequestMapping("/infoIntroduce/{usersId}")
    public R infoIntroduceByUserId(@PathVariable("usersId") Long usersId) {
        ArticleDetailVO vo = articleService.getIntroduceByUserId(usersId);
        return R.ok().put("data", vo);
    }

    @RequestMapping("info/desc")
    public R getDesc() {
        cacheArticleLinkedList.init();
        return R.ok().put("data", CacheArticleLinkedList.getCacheList());
    }
}
